﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;
namespace E02
{
    /// <summary>
    /// 返利政策
    /// </summary>
    class A12 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A12";
                List<E02A12M> ht = new List<E02A12M>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select FID BillId,FBillNo Number,case when FDate = '' then '1990-01-01 00:00:00' when FDate IS null then '1990-01-01 00:00:00' else FDate end CheckDate,case when FDate1 = '' then '1990-01-01 00:00:00' when FDate1 IS null then '1990-01-01 00:00:00' else FDate1 end BillDate,FCheckBox Executed,FCheckBox1 MergeCustomer," +
                             " FBigText Remark,b.FName Biller,c.FName Checker,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID from t_BOS_zhengce a " +
                             " left join t_User b on a.FBiller = b.FUserID and a.FBiller <> 0 " +
                             " left join t_User c on a.FUser = c.FUserID and a.FUser <> 0" +
                             " where a.FID = @FID ";
                sql = sql + " select FComboBox4 LeftBracket,FComboBox Condition,FComboBox1 Compare,FText CompareValue,FComboBox5 RightBracket," +
                            " FComboBox3 Legal from t_BOS_zhengceEntry where FID = @FID ";
                sql = sql + " select FComboBox7 ProjectName,fdecimal2 MinValue,FDecimal3 MaxValue,FDecimal CashReturn,FDecimal1 AcceptReturn," +
                            " FComboBox10 RebateType,FComboBox2 ReturnType from t_BOS_zhengceEntry2 where FID = @FID ";
                sql = sql + " select b.F_102 CustID,b.FName CustName from t_BOS_zhengceEntry3 a " +
                            " left join t_Organization b on a.fbase = b.FItemID where b.FItemID is not null and b.FItemID <> 0 and a.FID = @FID";
                
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        E02A12M e02A12M = new E02A12M();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A12M, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        List<E02A12E> conditionList = new List<E02A12E>();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        {
                            E02A12E e02A12E = new E02A12E();
                            for (int j = 0; j < rs.Data.Tables[1].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[1].Columns[j];
                                ReflectUtil.setValue(e02A12E, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            conditionList.Add(e02A12E);
                        }
                        e02A12M.ConditionList = handler.Json.ClassToJson(conditionList);

                        List<E02A12P> policyList = new List<E02A12P>();

                        foreach (DataRow entry in rs.Data.Tables[2].Rows)
                        {
                            E02A12P e02A12P = new E02A12P();
                            for (int j = 0; j < rs.Data.Tables[2].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[2].Columns[j];
                                ReflectUtil.setValue(e02A12P, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            policyList.Add(e02A12P);
                        }
                        e02A12M.PolicyList = handler.Json.ClassToJson(policyList);
                        ht.Add(e02A12M);

                        List<E02A12C> customerList = new List<E02A12C>();

                        foreach (DataRow entry in rs.Data.Tables[3].Rows)
                        {
                            E02A12C e02A12C = new E02A12C();
                            for (int j = 0; j < rs.Data.Tables[3].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[3].Columns[j];
                                ReflectUtil.setValue(e02A12C, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            customerList.Add(e02A12C);
                        }
                        e02A12M.CustomerList = handler.Json.ClassToJson(customerList);
                        ht.Add(e02A12M);

                        result.Content = handler.Json.ClassToJson(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("返利政策数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("返利政策数据提取错误:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 返利政策
    /// </summary>
    public class E02A12M
    {
        public Int32 BillId; //单据ID
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        /// <summary>
        /// 制单日期
        /// </summary>
        public String BillDate;
        /// <summary>
        /// 合并客户
        /// </summary>
        public Boolean MergeCustomer;
        /// <summary>
        /// 审核日期
        /// </summary>
        public String CheckDate;
        /// <summary>
        /// 已执行
        /// </summary>
        public Boolean Executed;
        /// <summary>
        /// 制单人
        /// </summary>
        public String Biller;
        /// <summary>
        /// 审核人
        /// </summary>
        public String Checker;

        /// <summary>
        /// 返利条件列表
        /// </summary>
        public String ConditionList;
        /// <summary>
        /// 返利政策列表
        /// </summary>
        public String PolicyList;
        /// <summary>
        /// 客户列表
        /// </summary>
        public String CustomerList;

        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
    }

    /// <summary>
    /// 返利条件单据体
    /// </summary>
    public class E02A12E
    {
        /// <summary>
        /// 左括号
        /// </summary>
        public String LeftBracket;
        /// <summary>
        /// 过滤条件
        /// </summary>
        public String Condition;
        /// <summary>
        /// 比较
        /// </summary>
        public String Compare;
        /// <summary>
        /// 比较值
        /// </summary>
        public String CompareValue;
        /// <summary>
        /// 右括号
        /// </summary>
        public String RightBracket;
        /// <summary>
        /// 逻辑
        /// </summary>
        public String Legal;
    }

    /// <summary>
    /// 返利政策单据体
    /// </summary>
    public class E02A12P
    {
        /// <summary>
        /// 项目名称
        /// </summary>
        public String ProjectName;
        /// <summary>
        /// 最小值
        /// </summary>
        public Decimal MinValue;
        /// <summary>
        /// 最大值
        /// </summary>
        public Decimal MaxValue;
        /// <summary>
        /// 现金返还
        /// </summary>
        public Decimal CashReturn;
        /// <summary>
        /// 承兑返还
        /// </summary>
        public Decimal AcceptReturn;  
        /// <summary>
        /// 返利模式
        /// </summary>
        public String ReturnType;
        /// <summary>
        /// 返还方式
        /// </summary>
        public String RebateType;
    }

    /// <summary>
    /// 非适用客户单据体
    /// </summary>
    public class E02A12C
    {
        /// <summary>
        /// 客户ID
        /// </summary>
        public String CustID;
        /// <summary>
        /// 客户名称
        /// </summary>
        public String CustName;
    }
}
